// File Name: led_ctrl.h
// Author: kukukukuku~
// Date: 2024-06-25

#ifndef __LED_CTRL_H
#define __LED_CTRL_H

#include "at32f421.h"

typedef struct LED
{
    gpio_type *gpioPort;
    uint32_t gpioPin;
    tmr_type *tmr;
    uint16_t tmrPulse;
    uint16_t tmrCurrentPulse;
    uint16_t breathStep;
    uint16_t blinkNum;
    uint16_t blinkCnt;
    gpio_pins_source_type gpioPinSource;
    gpio_mux_sel_type gpioMux;
    tmr_channel_select_type tmrCh;
    uint8_t mode : 2;
    uint8_t breathDir : 1;
    uint8_t state : 1;
}LED, *pLED;

#define LED1_GPIO_CRM           CRM_GPIOA_PERIPH_CLOCK
#define LED1_GPIO_PORT          GPIOA
#define LED1_GPIO_PIN           GPIO_PINS_4
#define LED1_GPIO_MUX_SOURCE    GPIO_PINS_SOURCE4
#define LED1_GPIO_MUX           GPIO_MUX_4

#define LED2_GPIO_CRM           CRM_GPIOB_PERIPH_CLOCK
#define LED2_GPIO_PORT          GPIOB
#define LED2_GPIO_PIN           GPIO_PINS_8
#define LED2_GPIO_MUX_SOURCE    GPIO_PINS_SOURCE8
#define LED2_GPIO_MUX           GPIO_MUX_2

#define LED1_TMR_CRM            CRM_TMR14_PERIPH_CLOCK
#define LED1_TMR                TMR14
#define LED1_TMR_DIV            (120)
#define LED1_TMR_PR_BLINK       (1000)
#define LED1_TMR_PR_BREATH      (5000)
#define LED1_TMR_PULSE          (5000)
#define LED1_TMR_CH             TMR_SELECT_CHANNEL_1
#define LED1_TMR_IRQ            TMR14_GLOBAL_IRQn
#define LED1_TMR_HANDLE         TMR14_GLOBAL_IRQHandler

#define LED2_TMR_CRM            CRM_TMR16_PERIPH_CLOCK
#define LED2_TMR                TMR16
#define LED2_TMR_DIV            (120)
#define LED2_TMR_PR_BLINK       (1000)
#define LED2_TMR_PR_BREATH      (5000)
#define LED2_TMR_PULSE          (5000)
#define LED2_TMR_CH             TMR_SELECT_CHANNEL_1
#define LED2_TMR_IRQ            TMR16_GLOBAL_IRQn
#define LED2_TMR_HANDLE         TMR16_GLOBAL_IRQHandler

#define LED_MODE_BREATH         (0)
#define LED_MODE_BLINK          (1)
#define LED_MODE_SWITCH         (2)

#define LED_STATE_ON            (1)
#define LED_STATE_OFF           (0)

#define LED_ENA                 (0)
#define LED_DISA                (1)

#define LED_BLINK_TIME_MIN      (50)    //LED闪烁最短时间(ms)
#define LED_BLINK_TIME_MAX      (2000)  //LED闪烁最长时间(ms)

#define LED_BREATH_STEP_MIN     (10)    //LED呼吸最小步进
#define LED_BREATH_STEP_MAX     (100)   //LED呼吸最大步进

extern LED led1, led2;  //LED对象

extern uint8_t led1Switch; //LED1开关
extern uint8_t led2Switch; //LED2开关

//打开LED
void led_on(pLED led);

//关闭LED
void led_off(pLED led);

//LED配置
void led_config(void);

#endif //__LED_CTRL_H
